package com.hd.core.project.module.controller;

import java.util.ArrayList;

import java.util.Arrays;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;


import com.hd.core.project.module.base.WrapperBaseController;
import com.hd.core.project.module.config.TeacherModuleProperties;
import com.hd.core.project.module.domain.Select2;
import com.hd.core.project.module.domain.CivilizedDormitory;
import com.hd.core.project.module.service.DormitoryService;
import com.hd.core.project.module.service.CivilizedDormitoryService;
import com.hd.core.project.module.utils.DateUtilsWrapper;

import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baomidou.mybatisplus.mapper.Condition;
import com.hd.core.framework.aspectj.lang.annotation.Log;
import com.hd.core.framework.aspectj.lang.constant.BusinessType;
import com.hd.core.framework.web.page.TableDataInfo;
import com.hd.core.framework.web.domain.AjaxResult;

/**
 * <p>文明宿舍-控制层</p>
 * <p>提供：基本CRUD操作和条件查询操作</p>
 * 
 * @since JDK 1.8
 * @version  V1.0
 * @author RichardTang 
 * @date: 2018年9月4日
 * @package com.hd.core.project.module.controller
 * @copyright:Copyright (c) 2018, 1245811923@qq.com All Rights Reserved.
 */
@Controller
@RequestMapping("/module/teacher/civilized-dormitory")
public class CivilizedDormitoryController extends WrapperBaseController<TeacherModuleProperties> {
	
	@Autowired
	private CivilizedDormitoryService civilizedDormitoryService;
	
	@Autowired
	private DormitoryService dormitoryService;
	
	CivilizedDormitoryController() {
		super("/civilized-dormitory");
	}
	
	/**
	 * <p>跳转到文明宿舍视图</p>
	 * <p>访问权限: module-teacher:civilized-dormitory:view</p>
	 * <p>路径: /templates/module/teacher/civilized-dormitory/civilized-dormitory.html</p>
	 * 
	 * @return 文明宿舍视图路径
	 */
	@GetMapping
	@RequiresPermissions("module-teacher:civilized-dormitory:view")
	public String view(){
	    return getResponseViewPath("/civilized-dormitory");
	}
	
	/**
	 * <p>查询文明宿舍<p/>
	 * <p>根据Entity实体内的成员属性的值来当做查询条件<p/>
	 * <p>并且拼接查询的时间范围,belong_date字段的范围</p>
	 * <p>访问权限: module-teacher:civilized-dormitory:list</p>
	 * 
	 * @param entity 条件实体
	 * @return 符合条件的文明宿舍数据
	 */
	@ResponseBody
	@PostMapping("/list")
	@RequiresPermissions("module-teacher:civilized-dormitory:list")
	public TableDataInfo list(CivilizedDormitory entity){
		startPage();
		Condition condition = new Condition();
		Map<String, Object> params = entity.getParams();
		if(StringUtils.isNotBlank(entity.getDormitoryNumber())) {
			condition.eq("dormitory_number", entity.getDormitoryNumber());
		}
		if(params != null && params.size() > 0) {
			Object beginTime = params.get("beginTime");
			Object endTime = params.get("endTime");
			if(beginTime != null && StringUtils.isNotBlank((String)beginTime)) {
				condition.ge("belong_date", beginTime);
			}
			if(beginTime != null && StringUtils.isNotBlank((String)endTime)) {
				condition.le("belong_date", endTime);
			}
		}
        List<CivilizedDormitory> list = civilizedDormitoryService.selectList(entity,condition);
		return getDataTable(list);
	}
	
	/**
	 * <p>跳转到新增文明宿舍功能视图</p>
	 * <p>路径: /templates/module/teacher/civilized-dormitory/add.html</p>
	 * 
	 * @return 文明宿舍添加功能视图
	 */
	@GetMapping("/add")
	public String add(){
	    return getResponseViewPath("/add");
	}
	
	/**
	 * <p>新增文明宿舍数据项<p/>
	 * <p>根据dormNumbers的大小来判断新增多少文明宿舍数据(可一次增加多个选项)<p/>
	 * <p>访问权限: module-teacher:civilized-dormitory:add</p>
	 * 
	 * @param dormNumbers 需要被选中为文明宿舍的宿舍号,可以一次接收多个宿舍号
	 * @param belongDate 本次添加的宿舍的所属的日期
	 * @param remark 备注信息
	 * @return http状态消息
	 */
	@ResponseBody
	@PostMapping("/add")
	@Log(title = "文明宿舍", action = BusinessType.INSERT)
	@RequiresPermissions("module-teacher:civilized-dormitory:add")
	public AjaxResult addSave(String[] dormNumbers,String belongDate,String remark){
		Set<String> dormNumberSet = new HashSet<String>(Arrays.asList(dormNumbers));
		Date date = DateUtilsWrapper.strFormatDate(DateUtilsWrapper.YYYY_MM_DD, belongDate);
		List<CivilizedDormitory> civilizedDorms = new ArrayList<CivilizedDormitory>();
		//没有填写日期时默认为今天
		if(date == null) {
			date = new Date();
		}
		for(String itemNumber : dormNumberSet) {
			civilizedDorms.add(
				new CivilizedDormitory(itemNumber,date,remark)
			);
		}
		boolean result = civilizedDormitoryService.insertBatch(civilizedDorms);
		return toAjax(result);
	}

	/**
	 * <p>跳转到修改文明宿舍功能视图</p>
	 * <p>根据URL上的id值,先查询出要被修改的文明宿舍,回现到页面上</p>
	 * <p>路径: /templates/module/teacher/civilized-dormitory/edit.html</p>
	 * @return 文明宿舍修改视图路径
	 */
	@GetMapping("/edit/{id}")
	public String edit(@PathVariable("id") Integer id, ModelMap mmap){
		CivilizedDormitory entity =civilizedDormitoryService.selectById(id);
		mmap.put("civilizedDormitory", entity);
	    return getResponseViewPath("/edit");
	}
	
	/**
	 * <p>修改并保存文明宿舍<p/>
	 * <p>根据实体的Id值来进行保存操作<p/>
	 * <p>访问权限: module-teacher:civilized-dormitory:edit</p>
	 * 
	 * @param entity 参数实体也是判断条件
	 * @param date 新的所属期数时间,没有传则不修改原先的值
	 * @return http状态消息
	 */
	@ResponseBody
	@PostMapping("/edit")
	@RequiresPermissions("module-teacher:civilized-dormitory:edit")
	@Log(title = "文明宿舍", action = BusinessType.UPDATE)
	public AjaxResult editSave(CivilizedDormitory entity,String date){
		if(StringUtils.isNotBlank(date)) {
			Date formatAfterDate = DateUtilsWrapper.strFormatDate(DateUtilsWrapper.YYYY_MM_DD, date);
			entity.setBelongDate(formatAfterDate);
		}
		return toAjax(civilizedDormitoryService.updateById(entity));
	}
	
	/**
	 * <p>根据文明宿舍选项的id删除文明宿舍<p/>
	 * <p>根据Id删除可以同时接收多个Id<p/>
	 * <p>访问权限: module-teacher:civilized-dormitory:remove</p>
	 * 
	 * @param ids 需要被删除的文明宿舍所对应的id号
	 * @return http状态消息
	 */
	@ResponseBody
	@PostMapping( "/remove")
	@RequiresPermissions("module-teacher:civilized-dormitory:remove")
	@Log(title = "文明宿舍", action = BusinessType.DELETE)
	public AjaxResult remove(Integer[] ids){		
		List<Integer> idsList = Arrays.asList(ids);
		return toAjax(civilizedDormitoryService.deleteBatchIds(idsList));
	}
	
	/**
	 * <p>查询宿舍表(module_student_dormitory_list)并将宿舍表的数据打包成Select2的格式{@link Select2}<p/>
	 * <p>注意这里是查询宿舍表而不是文明宿舍表,因为权限校验的问题所以在service层调用的依然是宿舍对应的业务功能<p/>
	 * <p>访问权限: module-teacher:civilized-dormitory:view</p>
	 * 
	 * @return http状态消息-序列化后的Select2格式数据
	 */
	@ResponseBody
	@GetMapping( "/queryDormFormatSelect2")
	@RequiresPermissions(
		value = {"module-teacher:civilized-dormitory:view"},
		logical = Logical.OR)
	public AjaxResult queryDormFormatSelect2(){
		List<Select2> result = 
				dormitoryService.selectList();
		return toSelect2Ajax(result);
	}
}
